import { lastIndexOf } from "lodash"

export const checkTel = (rule, value, callback) => {
  if (!value) {
    return callback()
  }
  const reg = /^1[3456789]\d{9}$/
  if (reg.test(value)) {
    return callback()
  }
  callback(new Error('请输入合法的手机号'))
}

export const checkNumber = (rule, value, callback) => {
  if (!value) {
    return callback()
  }
  const reg = /^[0-9]+$/
  if (reg.test(value)) {
    return callback()
  }
  callback(new Error('只能输入整数'))
}

export const checkDecimalOrNumber = (rule, value, callback) => {
  if (!value) {
    return callback()
  }
  if (value.toString().indexOf('.') !== value.toString().lastIndexOf('.') && value.toString().indexOf('.') !== 0) {
    return callback(new Error('请输入整数或小数'))
  }
  const reg = /^(([0-9]+)(\.[0-9]+))|(([0-9])+$)/
  if (reg.test(value)) {
    return callback()
  }
  callback(new Error('请输入整数或小数'))
}